﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Data;

namespace Brilliant.ORM
{
    /// <summary>
    /// 数据库帮助类
    /// </summary>
    public static class DBHelper
    {
        private static string connectionString;
        private static string providerName;
        private static IDataProvider dataProvider;
        private static PagedType dataPagedType;

        /// <summary>
        /// 数据库分页方式
        /// </summary>
        public static PagedType DataPagedType
        {
            get { return DBHelper.dataPagedType; }
        }

        /// <summary>
        /// 数据访问对象实例
        /// </summary>
        public static IDataProvider DataProvider
        {
            get { return DBHelper.dataProvider; }
        }

        /// <summary>
        /// 静态构造器
        /// </summary>
        static DBHelper()
        {
            ConnectionStringSettingsCollection csc = ConfigurationManager.ConnectionStrings;
            connectionString = csc["default"].ConnectionString;
            providerName = csc["default"].ProviderName;
            ProviderType dbType = ProviderType.SqlServer;
            if (!String.IsNullOrEmpty(providerName))
            {
                dbType = (ProviderType)Enum.Parse(typeof(ProviderType), providerName);
            }
            CreateProvider(dbType);
        }

        /// <summary>
        /// 返回一个新的DataParameter对象实例
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="value">参数值</param>
        /// <returns>DataParameter对象</returns>
        public static IDataParameter GetParameter(string parameterName, object value)
        {
            return dataProvider.GetParameter(parameterName, value);
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        public static void BeginTransaction()
        {
            dataProvider.BeginTransaction();
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public static void Commit()
        {
            dataProvider.Commit();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        public static void Rollback()
        {
            dataProvider.Rollback();
        }

        /// <summary>
        /// 改变数据库类型
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        public static void ChangeDbType(ProviderType dbType)
        {
            CreateProvider(dbType);
        }

        /// <summary>
        /// 更改连接属性
        /// </summary>
        /// <param name="name">属性名称</param>
        public static void ChangeConnectionString(string name)
        {
            connectionString = ConfigurationManager.ConnectionStrings[name].ConnectionString;
            providerName = ConfigurationManager.ConnectionStrings[name].ProviderName;
            ProviderType dbType = (ProviderType)Enum.Parse(typeof(ProviderType), providerName);
            CreateProvider(dbType);
        }

        /// <summary>
        /// 创建数据库访问对象
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        private static void CreateProvider(ProviderType dbType)
        {
            switch (dbType)
            {
                case ProviderType.Access:
                    dataProvider = new OleDb(connectionString);
                    break;
                case ProviderType.MySql:
                    try
                    {
                        Assembly assembly = Assembly.LoadFrom(@"Brilliant.DataProvider.MySql.dll");
                        Type type = assembly.GetType("Brilliant.ORM.MySql");
                        object obj = Activator.CreateInstance(type, connectionString);
                        dataProvider = (IDataProvider)obj;
                        dataPagedType = PagedType.Limit;
                    }
                    catch
                    {
                        throw new Exception("未能正确加载Brilliant.DataProvider.MySql.dll程序集");
                    }
                    break;
                case ProviderType.Oracle:
                    try
                    {
                        Assembly assembly = Assembly.LoadFrom(@"Brilliant.DataProvider.Oracle.dll");
                        Type type = assembly.GetType("Brilliant.ORM.Oracle");
                        object obj = Activator.CreateInstance(type, connectionString);
                        dataProvider = (IDataProvider)obj;
                        dataPagedType = PagedType.RowId;
                    }
                    catch
                    {
                        throw new Exception("未能正确加载Brilliant.DataProvider.Oracle.dll程序集");
                    }
                    break;
                case ProviderType.SQLite:
                    try
                    {
                        Assembly assembly = Assembly.LoadFrom(@"Brilliant.DataProvider.SQLite.dll");
                        Type type = assembly.GetType("Brilliant.ORM.SQLite");
                        object obj = Activator.CreateInstance(type, connectionString);
                        dataProvider = (IDataProvider)obj;
                        dataPagedType = PagedType.Limit;
                    }
                    catch
                    {
                        throw new Exception("未能正确加载Brilliant.DataProvider.SQLite.dll程序集");
                    }
                    break;
                case ProviderType.SqlServer:
                    dataProvider = new SqlServer(connectionString);
                    dataPagedType = PagedType.RowNumber;
                    break;
                default:
                    dataProvider = new SqlServer(connectionString);
                    dataPagedType = PagedType.RowNumber;
                    break;
            }
        }
    }

    /// <summary>
    /// 数据库类型枚举
    /// </summary>
    public enum ProviderType
    {
        /// <summary>
        /// Access数据库
        /// </summary>
        Access,

        /// <summary>
        /// MySql数据库
        /// </summary>
        MySql,

        /// <summary>
        /// Oracle数据库
        /// </summary>
        Oracle,

        /// <summary>
        /// SQLite数据库
        /// </summary>
        SQLite,

        /// <summary>
        /// SqlServer数据库
        /// </summary>
        SqlServer
    }

    /// <summary>
    /// 分页类型
    /// </summary>
    public enum PagedType
    {
        /// <summary>
        /// 用于Oracle数据库分页
        /// </summary>
        RowId,

        /// <summary>
        /// 用于SQL Server2005以及Oracle数据库分页
        /// </summary>
        RowNumber,

        /// <summary>
        /// 用于MySql以及SQLite数据库分页
        /// </summary>
        Limit
    }
}
